07. Exercise: Connecting to the Internet

L8 08 Connecting To The Internet SCA V1

Download or check out the starter app:

Before you begin the first exercise, you'll either need to download the Mars Real Estate App starter app, clone the GitHub repo, checking out the starter-code branch, or you can download this exercise from: Step.01-Exercise-Making-Mars-Web-Service-Contact. Downloading the exercise will give you //TODO comments to help you complete the steps. If you get stuck, you can always go back and watch the video again.

Now let's connect to the internet!

In this first step you'll add the Retrofit library so your app can talk to the Mars web service.

When you've completed the exercise, your app displays the returned JSON string for a successful request, or an error message if the request fails.

  1. In app/build.gradle add the Retrofit library dependencies.

    The variables that specify the library versions are defined in the project level build.gradle file. The versions we're using work well together, and, most importantly, in this app.

    implementation "com.squareup.retrofit2:retrofit:$version_retrofit"
    implementation "com.squareup.retrofit2:converter-scalars:$version_retrofit"

Note: The provided example code and project uses Retrofit Library 2.9.0. It is a third party library and a great addition to the repertoire of skill sets. For more information, refer to appropriate Retrofit documentation if using an alternate version. For the latest Retrofit version and document, see: https://square.github.io/retrofit/


  1. Open MarsApiService.kt. We've provided a variable there for the root web address of the Mars server endpoint:
private const val BASE_URL = "https://mars.udacity.com/"


  1. Use Retrofit.Builder to create the Retrofit object.

    Add an instance of ScalarsConverterFactory and the BASE_URL we provided, then call build() to create the Retrofit object.

 private val retrofit = Retrofit.Builder()
        .addConverterFactory(ScalarsConverterFactory.create())
        .baseUrl(BASE_URL)
        .build()
    


  1. Create a MarsApiService interface, and define a getProperties() method to request the JSON response string.

    Annotate the method with @GET, specifying the endpoint for the JSON real estate response, and create the Retrofit Call object that will start the HTTP request.

interface MarsApiService {
    @GET("realestate")
    fun getProperties(): 
            Call<String>
}



  1. Passing in the service API you just defined, create a public object called MarsApi to expose the Retrofit service to the rest of the app:
 object MarsApi {
    val retrofitService : MarsApiService by lazy { 
       retrofit.create(MarsApiService::class.java) 
    }
}


  1. In OverviewViewModel.kt, use MarsApi.retrofitService to enqueue the Retrofit request in getMarsRealEstateProperties(), overriding the required Retrofit callbacks to assign the JSON response or an error message to the _response LiveData value. Make sure to import the Retrofit versions of Callback, Call, and Response.
 MarsApi.retrofitService.getProperties().enqueue( object: Callback<String> {
     override fun onFailure(call: Call<String>, t: Throwable) {
         _response.value = "Failure: " + t.message
     }

    override fun onResponse(call: Call<String>, response: Response<String>) {
        _response.value = response.body()
    }
})


  1. ** Don't forget to delete the response assignment placeholder, or you won't see your results!**
 _response.value = "Set the Mars API Response here!"


  1. Finally, in AndroidManifest.xml, add the internet permission with the uses-permission tag inside of the root manifest tag.
    <uses-permission android:name="android.permission.INTERNET" />



  1. Build and run your app and you'll see the JSON response as a big string that fills up the screen. Connectivity with the web service is, therefore, working, and we can continue on to do more interesting things with our app.

Once you’re done, you can check your solution against the solution we’ve provided here: Step.01-Solution-Making-Mars-Web-Service-Contact, or using this git diff.

Task Description:

Complete the tasks below to connect your app to the internet.

Task List:

Task Feedback:

Great job, now let's go shopping!

Reference documentation